<?php

namespace app\data\logic;

use app\data\service\FriendlinkService;

class FriendlinkLogic extends BaseLogic
{
    protected FriendlinkService $friendlinkService;

    public function __construct()
    {
        $this->friendlinkService = new FriendlinkService();
    }

    /**
     * 友链分类列表
     * @param $params
     * @return array
     */
    public function getFriendlinkList($params)
    {
        $field = '*';
        $where = [];
        $order = 'link_id desc';
        $withs = [];
        $pageIndex = $params['page'] ?? 1;
        $pageSize = $params['limit'] ?? 10;
        $result = $this->friendlinkService->getPageList($pageIndex, $pageSize, $where, $field, $order, $withs);
        if (array_key_exists('all', $params)) {
            $result = $this->friendlinkService->getAll($field, $where, $order, $withs);
        }

        return $result;
    }


    /**
     * 添加友链
     * @param $data
     * @return array
     */
    public function createNewFriendlink($data): array
    {
        try {
            $ret = $this->friendlinkService->insert($data);
            if ($ret) {
                return $this->responseInfo(true, 'SUCCESS');
            } else {
                return $this->responseInfo(false, 'FAILURE');
            }
        } catch (\Exception $e) {
            return $this->responseInfo(false, $e->getMessage());
        }
    }

    /**
     * 更新友链
     * @param $id
     * @param $data
     * @return array
     */
    public function updateFriendlink($id, $data): array
    {
        try {
            $where = [
                'link_id' => $id
            ];
            $ret = $this->friendlinkService->update($where, $data);
            if ($ret) {
                return $this->responseInfo(true, 'SUCCESS');
            } else {
                return $this->responseInfo(false, 'FAILURE');
            }
        } catch (\Exception $e) {
            return $this->responseInfo(false, $e->getMessage());
        }
    }

    /**
     * 友链详情
     * @param $id
     * @return array
     */
    public function getFriendlinkDetail($id): array
    {
        try {
            $where = [
                'link_id' => $id
            ];
            $withs = [];
            $res = $this->friendlinkService->getOne('*', $where, $withs);
            if ($res) {
                return $this->responseInfo(true, 'SUCCESS', $res);
            } else {
                return $this->responseInfo(false, 'FAILURE');
            }
        } catch (\Exception $e) {
            return $this->responseInfo(false, $e->getMessage());
        }
    }

    /**
     * 删除友链
     * @param $id
     * @return array
     */
    public function deleteFriendlink($id): array
    {
        try {
            $where = [
                'link_id' => $id
            ];
            $ret = $this->friendlinkService->softDel($where);
            if ($ret) {
                return $this->responseInfo(true, 'SUCCESS');
            } else {
                return $this->responseInfo(false, 'FAILURE');
            }
        } catch (\Exception $e) {
            return $this->responseInfo(false, $e->getMessage());
        }
    }

}